﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<form id="form1">
    <div style="padding-top: 20px; margin-right: 20px;">
        <table class="form">
            <tr>
                <th class="formTitle">租户账号</th>
                <td class="formValue">
                    <input id="TenantName" name="TenantName" type="text" class="form-control required" placeholder="请输入微信号" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">租户名称</th>
                <td class="formValue">
                    <input id="FullName" name="FullName" type="text" class="form-control required" placeholder="请输入公众号名称" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">允许登录</th>
                <td class="formValue">
                    <select id="EnabledMark" name="EnabledMark" class="form-control required">
                        <option value="true">是</option>
                        <option value="false">否</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th class="formTitle" valign="top" style="padding-top: 5px;">
                    备注
                </th>
                <td class="formValue">
                    <textarea id="Description" name="Description" class="form-control" style="height: 100px;"></textarea>
                </td>
            </tr>
        </table>
    </div>
</form>
<script>
    var keyValue = $.request("keyValue");
    $(function () {
        initControl();
        if (!!keyValue) {
            $.ajax({
                url: "/SystemManage/Tenant/GetFormJson",
                data: { keyValue: keyValue },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#TenantName").attr('disabled', 'disabled');
                    $("#form1").formSerialize(data);
                }
            });
        }
    });
    function initControl() {
        $("#EnabledMark").bindSelect();
    }
    function submitForm() {
        if (!$('#form1').formValid() || !validTenantNameFormat()) {
            return false;
        }
        $.submitForm({
            url: "/SystemManage/Tenant/SubmitForm?keyValue=" + keyValue,
            param: $("#form1").formSerialize(),
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }
    function validTenantNameFormat() {
        var tenantname = $("#TenantName").val();
        if (tenantname == "" || tenantname == null) {
            $.modalMsg("租户名不能为空，请填写租户名。", "error");
            return false;
        }
        else if (/^\d.*$/.test(tenantname)) {
            $.modalMsg("您输入的租户名“" + tenantname + "”格式不正确，租户名不能以数字开头。", "error");
            return false;
        }
        else if (! /^.{6,20}$/.test(tenantname)) {
            $.modalMsg("您输入的租户名“" + tenantname + "”字符长度有误，合法长度为6-20个字符。", "error");
            return false;
        }
        else if (! /^[\w_]*$/.test(tenantname)) {
            $.modalMsg("您输入的租户名“" + tenantname + "”含有非法字符，用户名只能包含_,英文字母，数字。", "error");
            return false;
        }
        else if (! /^([a-z]|[A-Z])[\w_]{5,19}$/.test(tenantname)) {
            $.modalMsg("您输入的租户名“" + tenantname + "”格式不正确，用户名只能包含_,英文字母，数字。", "error");
            return false;
        }
        else {
            return true;
        }
    }
</script>